import axios from "axios";
import { ElMessage } from 'element-plus'
import VueCookies from 'vue-cookies'
const Cookies: any = VueCookies


const BASEURL = {
  DEV: 'http://10.199.66.99:8080/com.neu',
  PRO: '',
  TEST: '',
}

const getBaseUrl = () => {
  let url = ''
  switch (process.env.NODE_ENV) {
    // 生产环境，部署到服务器上的环境
    case 'production':
      url = BASEURL.PRO
      break;
    //设置测试环境的接口地址
    case 'test':
      url = BASEURL.TEST
      break;
    //开发环境接口地址
    default:
      url = BASEURL.DEV
  }
  return url
}

// import { useRouter } from 'vue-router';
// const redirect = (path: string) => {
//   const router = useRouter();
//   router.replace(path);
// }

import { ElLoading } from 'element-plus'//节流阀
const options = {
  text: '正在请求数据中,莫急...',
  background: 'rgba(0, 0, 0, 0.7)',
  //   svg: `
  //   <path class="path" d="
  //     M 30 15
  //     L 28 17
  //     M 25.61 25.61
  //     A 15 15, 0, 0, 1, 15 30
  //     A 15 15, 0, 1, 1, 27.99 7.5
  //     L 15 15
  //   " style="stroke-width: 4px; fill: rgba(0, 0, 0, 0)"/>
  // `,//图标
}
let loading: any
function openLoading() {
  //开启
  loading = ElLoading.service(options)
}
function closeLoading() {
  //关闭
  loading.close()
}

export const http = (options: any) => {
  return new Promise((resolve, reject) => {
    // create an axios instance
    const service = axios.create({
      baseURL: getBaseUrl(), // api 的 base_url 注意 vue3 .env文件有强制性的 VITE_ 开头
      timeout: 10000,
    });
    // request interceptor
    service.interceptors.request.use(
      (config: any) => {
        openLoading()

        let token: string = Cookies.get('token') ? Cookies.get('token') : ''; //此处换成自己获取回来的token，通常存在在cookie或者store里面
        if (token) {
          config.headers["token"] = token;
          config.headers.Authorization = +token;
        }
        return config;
      },
      (error) => {
        console.log("出错啦", error);
        Promise.reject(error);
      }
    );

    // response interceptor
    service.interceptors.response.use(
      (response) => {
        closeLoading()
        if (response.data && response.data.code == 403) {
          ElMessage.error("没有足够的权限----code:403");
        }
        else if (response.data && response.data.code == 401) {
          ElMessage.error("登录状态过期----code:401");
          // redirect('/login')
        }
        else if (response.data && response.data.code == 404) {
          ElMessage.error("无法访问----code:404");
          // router.replace('/notFound')
        } else if (!response) {
          ElMessage.error("服务器请求错误，请稍后再试");
        }
        return response.data;
      },
      (error) => {
        console.log("err" + error); // for debug
        if (error.response) {
          if (error.response.data.code == 403) {
            ElMessage.error("没有足够的权限----code:403");
          }
          else if (error.response.data.code == 401) {
            ElMessage.error("登录状态过期----code:401");
            // router.replace('/login')
          }
          else if (error.response.data.code == 404) {
            ElMessage.error("无法访问----code:404");
            // router.replace('/notFound')
          } else {
            ElMessage.error("服务器请求错误，请稍后再试");
          }
          return Promise.reject(error);
        } else {
          ElMessage.error(`网络错误...${error}`)
        }

      }
    );
    // 请求处理
    service(options)
      .then((res) => {
        resolve(res);
      })
      .catch((error) => {
        reject(error);
      });
  });
};

export default http;